package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.entity.bo.FinanceInfoBo;
import com.mf.entity.bo.OtherInfoBo;
import com.mf.entity.vo.OtherInfoVo;
import com.mf.mapper.OtherInfoMapper;
import com.mf.service.IOtherInfoService;
import io.github.heollhai.common.utils.MybatisPlusUtils;
import io.github.heollhai.common.utils.VerifyUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 服务实现类
 *
 * @author mf
 * @since 2024-09-01
 */
@Service
public class OtherInfoServiceImpl extends ServiceImpl<OtherInfoMapper, OtherInfoBo> implements IOtherInfoService {
    @Resource
    private OtherInfoMapper otherInfoMapper;

    /**
     * 查询
     */
    @Override
    public OtherInfoVo getByRosterId(Integer rosterId) {
        LambdaUpdateWrapper<OtherInfoBo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OtherInfoBo::getRosterId, rosterId);
        return MybatisPlusUtils.changeDataClass(otherInfoMapper.selectOne(wrapper), OtherInfoVo.class);
    }

    /**
     * 新增|编辑
     */
    @Override
    public Integer insert(OtherInfoVo otherInfoVo) {
        final LambdaUpdateWrapper<OtherInfoBo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OtherInfoBo::getRosterId, otherInfoVo.getRosterId());

        OtherInfoBo otherInfoBo = otherInfoMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(otherInfoBo)) {
            otherInfoBo = new OtherInfoBo();
        }
        BeanUtils.copyProperties(otherInfoVo, otherInfoBo);

        if (VerifyUtils.isEmpty(otherInfoVo.getId())) {
            return otherInfoMapper.insert(otherInfoBo);
        }

        return otherInfoMapper.updateById(otherInfoBo);
    }
}
